from fastapi import Depends, Request
from afast_core.core_tools.jwt_helper import SimpleAuth
from infirmary_admin_src.infirmary_common.infirmary_errors.http_exception import SnowyPermissionDenied


async def check_login(request: Request) -> Request:
    # 免登入验证的API接口
    if request.url.path not in ["/business_cms_api/sys/authv2", "/business_cms_api/sys/randomImage/1629428467008",
                                "/docs", "/openapi.json"]:
        pass
        token = request.headers.get("token")
        if not token:
            raise SnowyPermissionDenied(message="当前没有访问权限！")

        isok, state, token_userinfo_result = SimpleAuth.verify_bearer_token_state(token=token)
        if not isok and (state == 1 or state == 2):
            raise SnowyPermissionDenied(message=token_userinfo_result)

        # 写入当前请求上下的当前对象
        request.state.token_userinfo_result = token_userinfo_result

        token = request.state.token_userinfo_result
        # 用户ID 写入上下文字
        print('token.get('')', token.get('account'))
        request.state.account = token.get('account')
        request.state.account_id = token.get('account_id')
        request.state.org_id = token.get('org_id')
        request.state.position_id = token.get('position_id')

        return request
